import React, { Component } from 'react'
import Header from "../components/all/header";
import Footer from "../components/all/footer";
import { apilink } from '../api/apilink';
import Oneorder from "../components/order/oneorder"
import {SOrder} from "../styled/order"
import { withRouter } from 'react-router-dom';
class orderForGoods extends Component {
    constructor(props){
        super(props)
        this.state={
            order_goods:"",//总数据
            order_pay:"",
            totals_money:""
        }
    }
    componentDidMount(){
        //发送请求，获取到被选中的数据， 然后进行遍历，生成订单。
        let usp = new URLSearchParams();
        usp.append("userId",sessionStorage.getItem("userId"));
        apilink("/api/get_order_checked_goods","post",usp).then((ok)=>{
            console.log(ok.data);
            this.setState({
                order_goods:ok.data,
            })
        }).catch((err)=>{
            alert("出错，请重试！")
        })
        // let dingdan_data = JSON.parse(localStorage.getItem("dingdan"));
        // console.log(dingdan_data)
        // this.setState({
        //         order_goods:dingdan_data,
        // })
    }
    //未支付
    one_dingdan=()=>{
        // let newarr = []
        // this.state.order_goods.forEach((item,index)=>{
        //     if(item.payStatus == 0){
        //         newarr.push(item);
        //     }
        // })
        // console.log(newarr)
        // this.totalsMoney()
        return this.state.order_goods.map((item,index)=>{
            return (<Oneorder orderInfo={item} key={index}></Oneorder>)
        })
    }
    //修改总价
    totalsMoney=()=>{
        // 累计的小计
        let totals_moneys = 0;
        this.state.order_goods.forEach((item,index)=>{
            totals_moneys = totals_moneys+Number(item.totalsMoney)
        })
        localStorage.setItem("totals_moneys",totals_moneys)
        return totals_moneys
        // this.setState({
        //     totals_money:totals_moneys
        // },()=>{
        // })
    }
    //点击结算按钮执行的逻辑、
    pay=()=>{
        let password=prompt("请输入支付密码")
        if(password){
                this.state.order_goods.forEach((item,index)=>{
                    let usp = new URLSearchParams();
                        usp.append("userId",sessionStorage.getItem("userId"));
                        usp.append("listGoodsId",item.listGoodsId);
                        usp.append("payStatus",1)
                    apilink("/api/uppay_status","post",usp).then((ok)=>{
                        console.log(ok)
                        console.log(password);
                        if(ok.data === "pay_success"){
                            this.props.history.push("/paySuccess")
    
                        }
                    }).catch((err)=>{
                        console.log(err)
                    })
                    
                })
            }else{

            }
    }
    render() {
        return (
            <div>
                <Header></Header>
                <div>
                    <SOrder>
                    <h1>我的订单</h1>
                    <div className="onegoods">
                        <div className="order">

                        {this.state.order_goods?this.one_dingdan():""}
                        <h2 className="totals_zong">总价：{this.state.order_goods?this.totalsMoney.bind(this)():""}</h2>
                        <button className="totals_money" onClick={this.pay.bind(this)}>结算</button>
                        </div>
                        {/* 地址栏 */}
                        <div className="address" >
                            <img src="images/images/address.jpg" alt="加载失败" />
                        </div>
                    </div>
                    </SOrder>
                </div>
                <Footer></Footer>
            </div>
        )
    }
}
export default withRouter(orderForGoods)